package com.chao.cloud.controller;

import com.chao.entities.CommonResult;
import com.chao.entities.Payment;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 * @date 2021-06-08 14:04
 */
@RestController
public class PayMentController {

    @Value("${server.port}")
    private Integer serverPort;


    @PostMapping(value = "/payment/creat")
    public CommonResult creatPayment(@RequestBody Payment payment) {
        //保存到数据库
        return new CommonResult(200,"保存成功");
    }

    @GetMapping(value = "/payment/get/{id}")
    @HystrixCommand/*(fallbackMethod = "fallbackResult",commandProperties={
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value="10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value="10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value="60")
    })*/
    public CommonResult getPayment(@PathVariable("id") Long id){
        if (id < 0) {
            throw new RuntimeException("*****************id不能是负数");
        }
        Payment payment = new Payment();
        payment.setId(id);
        payment.setSerial("1111");
        return new CommonResult(200,"查询成功",payment);
    }

    public CommonResult fallbackResult(@PathVariable("id") Long id){
        return new CommonResult(500,"id不能为负数，请稍后再试,id：" + id);
    }

    @GetMapping(value = "/payment/getPort")
    public Integer getPort(){
        return serverPort;
    }
}
